package net.jueb.protobuf.server;

import net.jueb.protobuf.pojo.RequestProto.Request;
import net.jueb.protobuf.pojo.ResponseProto;
import net.jueb.protobuf.pojo.ResponseProto.Response;
import io.netty.channel.ChannelHandlerAdapter;
import io.netty.channel.ChannelHandlerContext;

public class ProtobufServerHandler extends ChannelHandlerAdapter{

	private int count;
	
	@Override
	public void channelRead(ChannelHandlerContext ctx, Object msg)
			throws Exception {
		Request request=(Request)msg;
		if(request==null)
		{
			return;
		}
		count++;
		System.out.println("收到请求:"+request.toString());
		
		//获取对象构造器
		ResponseProto.Response.Builder builder=ResponseProto.Response.newBuilder();
		//构造属性值
		builder.setId(count);
		builder.setResult("Hello Client!");
		//构造对象
		Response response =builder.build();
		
		
		ctx.writeAndFlush(response);
	}
	
	@Override
	public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause)
			throws Exception {
		cause.printStackTrace();
		ctx.close();
	}
}
